package main

import (
	"io"
	"log"
	"net"
)

const maxLength = 1024

func tcpClientInit() {
	conn, err := net.Dial("tcp", "127.0.0.1:9001")
	if err != nil {
		log.Println("连接服务器失败:", err)
		return
	}
	defer conn.Close()

	_, err = conn.Write([]byte("hello"))
	if err != nil {
		log.Println("发送数据失败:", err)
		return
	}

	//接收部分
	buf := make([]byte, maxLength)
	var receivedLength int

	for {
		n, err := conn.Read(buf)
		receivedLength += n

		if err != nil {
			if err != net.ErrClosed || err != io.EOF {
				log.Println("读取数据失败：", err)
			}
			break
		}

		log.Println("从服务器接收:", string(buf[:n]))
	}
}
